package com.sychip.fhc.app.drawer.screen.components

import androidx.compose.animation.core.MutableTransitionState
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TooltipState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.sychip.fhc.app.todo.R


private class DialogStateImpl(
    initialIsVisible: Boolean,
) : DialogState {
    override val transition: MutableTransitionState<Boolean> = MutableTransitionState(initialIsVisible)

    override val isVisible: Boolean
        get() = transition.currentState || transition.targetState


    override suspend fun show() {
        transition.targetState = true
    }

    /** Dismiss the tooltip associated with this [TooltipState] if it's currently being shown. */
    override fun dismiss() {
        transition.targetState = false
    }

}

/**
 * Dialog state
 *
 * @constructor Create empty Dialog state
 */
interface DialogState {
    /**
     * The current transition state of the tooltip. Used to start the transition of the tooltip when
     * fading in and out.
     */
    val transition: MutableTransitionState<Boolean>

    /** [Boolean] that indicates if the tooltip is currently being shown or not. */
    val isVisible: Boolean


    /**
     * Show
     *
     */
    suspend fun show()

    /**
     * Dismiss
     *
     */
    fun dismiss()


}

/**
 * Remember dialog state
 *
 * @param initialIsVisible
 * @return
 */
@Composable
fun rememberDialogState(
    initialIsVisible: Boolean = false
): DialogState =
    remember() {
        DialogStateImpl(
            initialIsVisible = initialIsVisible,
        )
    }

/**
 * Dialog with image
 *
 * @param onDismissRequest
 * @param onConfirmation
 * @param painter
 * @param state
 * @receiver
 * @receiver
 */
@Composable
fun DialogWithImage(
    onDismissRequest: () -> Unit ={},
    onConfirmation: () -> Unit={},
    painter: Painter = painterResource(id = R.drawable.logo_no_fill),
    state:DialogState,
) {
//    var showDialog by remember {mutableStateOf(true) }
    if(state.isVisible){
        Dialog(onDismissRequest = { onDismissRequest() }) {
            // Draw a rectangle shape with rounded corners inside the dialog
            Card(
                modifier = Modifier.fillMaxWidth().padding(16.dp),
                shape = RoundedCornerShape(16.dp),
            ) {
                Column(
                    modifier = Modifier.fillMaxSize(),
                    verticalArrangement = Arrangement.Center,
                    horizontalAlignment = Alignment.CenterHorizontally,
                ) {
                    Image(
                        painter = painter,
                        contentDescription = null,
                        contentScale = ContentScale.Fit,
                        modifier = Modifier.height(60.dp)
                    )
                    Text(
                        text = "带按钮和图片的弹框",
                        modifier = Modifier.padding(16.dp),
                    )
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.Center,
                    ) {
                        TextButton(
                            onClick = {
                                onDismissRequest()
                                state.dismiss()
//                                showDialog = !showDialog
                            },
                            modifier = Modifier.padding(8.dp),
                        ) {
                            Text("取消")
                        }
                        TextButton(
                            onClick = { onConfirmation()
                                state.dismiss()
//                                showDialog = !showDialog
                            },
                            modifier = Modifier.padding(8.dp),
                        ) {
                            Text("确认")
                        }
                    }
                }
            }
        }
    }
}